/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.bar;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.geom.Point2D;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import simplemetrosystem.model.charlanguage.NameMap;
import simplemetrosystem.view.Layout.GBC;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.view.canvas.MouseHandlers.MouseTrans;

/**
 *
 * @author Eric Qian
 */
public class FloorPanel extends JPanel {

    /**
     * 
     * @param frame
     */
    public FloorPanel(MetroFrame frame) {
        super();
        this.frame = frame;
        model = false;
        nameMap = NameMap.getInstance();
        this.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
        initComponent();
    }

    /**
     * 
     */
    private void initComponent() {
        this.setLayout(new GridBagLayout());
        add(this.getModelDisployLabel(),
                new GBC(0, 0).setAnchor(GBC.WEST).setInsets(0, 10, 0, 10));
        add(getPointDisployLabel(),
                new GBC(1, 0).setAnchor(GBC.CENTER).setInsets(0, 10, 0, 10));
        add(getSizeSlider(),
                new GBC(2, 0).setWeight(1, 0).setAnchor(GBC.EAST).setInsets(0, 10, 0, 10));
        add(this.getInfDisployLabel(),
                new GBC(3, 0).setWeight(3, 0).setAnchor(GBC.EAST).setInsets(0, 10, 0, 10));
    }

    /**
     * 
     * @return
     */
    public JPanel getPointDisployLabel() {
        if (pointDisployPanel == null) {
            pointDisployPanel = new JPanel();
            pointDisployPanel.setLayout(new GridBagLayout());
            xLabel = new JLabel("0");
            //xLabel.setPreferredSize(new Dimension(100, this.getHeight()));
            yLabel = new JLabel("0");
            // yLabel.setPreferredSize(new Dimension(100, this.getHeight()));

            pointDisployPanel.add(new JLabel("X: "), new GBC(0, 0).setInsets(0, 2, 0, 2));
            pointDisployPanel.add(xLabel,
                    new GBC(1, 0).setAnchor(GBC.EAST).setWeight(1, 0).setInsets(0, 2, 0, 2));
            pointDisployPanel.add(new JLabel("Y: "), new GBC(2, 0).setInsets(0, 2, 0, 2));
            pointDisployPanel.add(yLabel,
                    new GBC(3, 0).setAnchor(GBC.EAST).setWeight(1, 0).setInsets(0, 2, 0, 2));

        //pointDisployPanel.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
        }
        return pointDisployPanel;
    }

    /**
     * 
     * @return
     */
    public JSlider getSizeSlider() {
        if (sizeSlider == null) {
            sizeSlider = new JSlider(MIN, MAX, INITIAL);
            sizeSlider.setMajorTickSpacing(MAJORTICK);
            sizeSlider.setMinorTickSpacing(MINORTICK);
            sizeSlider.setPaintTicks(true);
            sizeSlider.setSnapToTicks(true);
            sizeSlider.addChangeListener(new SliderChangerListener(frame));

        }
        return sizeSlider;
    }

    /**
     * 
     * @return
     */
    public JPanel getModelDisployLabel() {
        if (modelDisployPanel == null) {
            modelDisployPanel = new JPanel();
            modelLabel = new JLabel(nameMap.getName(FloorPanel.EDIT_MODEL));
            modelDisployPanel.add(modelLabel);
        //modelDisployPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        }
        return modelDisployPanel;
    }

    public JPanel getInfDisployLabel() {
        if (infDisployPanel == null) {
            infDisployPanel = new JPanel();
            infLabel = new JLabel("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            infDisployPanel.setLayout(new GridBagLayout());
            infDisployPanel.add(new JLabel(nameMap.getName("Message") + ": "),
                    new GBC(0, 0).setWeight(0, 0).setAnchor(GBC.WEST));
            infDisployPanel.add(infLabel, new GBC(1, 0).setWeight(1, 0));
        //modelDisployPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        }
        return infDisployPanel;
    }

    public void setModel(boolean model) {
        if (this.model != model) {
            this.model = model;
            if (model) {
                modelLabel.setText(nameMap.getName(CALCUL_MODEL));
            } else {
                modelLabel.setText(nameMap.getName(EDIT_MODEL));
            }
        }
    }

    /**
     * 
     * @param point
     */
    public void setPoint(Point2D point) {
        getPointDisployLabel();
        xLabel.setText("" + point.getX());
        yLabel.setText("" + point.getY());
    }
    private MetroFrame frame;
    private NameMap nameMap;
    private JPanel pointDisployPanel;
    private JLabel xLabel;
    private JLabel yLabel;
    private JSlider sizeSlider;
    private JPanel modelDisployPanel;
    private JLabel modelLabel;
    private boolean model;
    private JPanel infDisployPanel;
    private JLabel infLabel;
    public static final int MIN = 3;
    public static final int MAX = 31;
    public static final int INITIAL = 7;
    public static final int MAJORTICK = 4;
    public static final int MINORTICK = 2;
    public static final String EDIT_MODEL = "Edit Model";
    public static final String CALCUL_MODEL = "Calculation Model";
}

/**
 * 
 * @author Eric Qian
 */
class SliderChangerListener implements ChangeListener {

    public SliderChangerListener(MetroFrame frame) {
        this.frame = frame;
    }

    public void stateChanged(ChangeEvent e) {
        JSlider slider = (JSlider) e.getSource();
        int value = slider.getValue();
        frame.getMetroTabbedPane().setMatrixSize(value);
        MouseTrans.NewInstance(value);
    }
    private MetroFrame frame;
}